package com.example.extendsview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * author：sunweihao
 * 创建时间：2021/1/9 12:10
 * Describe：
 */
public class IAmSoCuteView extends View {

    private int mAge;
    private String mName;
    private Paint mPaint;
    private Bitmap mBitmap;

    public IAmSoCuteView(Context context) {
        this(context, null);
    }

    public IAmSoCuteView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IAmSoCuteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //反射,对R.styleable上的值进行反射到 这里
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IAmSoCuteView);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int index = typedArray.getIndex(i);
            switch (index) {
                case R.styleable.IAmSoCuteView_my_age:
                    mAge = typedArray.getInt(index, 0);
                    break;
                case R.styleable.IAmSoCuteView_my_name:
                    mName = typedArray.getString(index);

                    break;
                case R.styleable.IAmSoCuteView_my_photo:
                    Drawable drawable = typedArray.getDrawable(index);
                    BitmapDrawable bitmapDrawable= (BitmapDrawable) drawable;
                    mBitmap = bitmapDrawable.getBitmap();
                    break;
            }

        }
        mPaint = new Paint();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.drawText(mName,50,50,mPaint);
        canvas.drawBitmap(mBitmap,50,50,mPaint);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
//        top=500;
//        top=500;
//        top=500;
//        top=500;
//        changed=true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
